package com.scenic.rd_android.filelock;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.*;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.nova.scenic.projectlibs.util.BaseActivity;
import com.scenic.rd_android.R;
import com.scenic.rd_android.servicetest.IMyAidlInterface;
import com.scenic.rd_android.servicetest.RemoteService;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.lang.Process;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class FileLock1 extends BaseActivity {


    public static FileLock getFileLock(File file) throws IOException {
        RandomAccessFile fi = new RandomAccessFile(file, "rw");
        FileChannel fc = fi.getChannel();
        return fc.tryLock();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_lock1);

        Button btnLock = (Button) findViewById(R.id.button6);
        Button btnRelease = (Button) findViewById(R.id.button7);
        Button btnWriteFile = (Button) findViewById(R.id.button8);
        btnWriteFile.setText("wirte file");

        int pid = android.os.Process.myPid();
        btnLock.setText("MyPID is " + pid);
        btnRelease.setText("Release");


        final File wirteFile = new File(Environment.getExternalStorageDirectory(), "ddd.txt");
        final FileLock[] ff = {null};

        btnLock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
//                        ff[0] = getFileLock(wirteFile);
                   FileOutputStream output = new FileOutputStream(wirteFile);
                   ff[0] = output.getChannel().lock();
                    Toast.makeText(FileLock1.this, "文件锁定了", Toast.LENGTH_SHORT).show();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
                    writer.write("我得测试");
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });


        btnWriteFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ServiceConnection conn = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        Toast.makeText(FileLock1.this,"connect sucess",Toast.LENGTH_LONG).show();
                        IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                        try {
                           String dd = iMyAidlInterface.execute(2);
                            System.out.println("result dd " + dd);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {

                    }
                };
                bindService(new Intent(FileLock1.this, RemoteService.class),
                        conn, Context.BIND_AUTO_CREATE);
            }
        });

        btnRelease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ff[0].release();
                    Toast.makeText(FileLock1.this,"文件释放了",Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
